package com.wuyou.simple;

/**
 * 1343. 大小为 K 且平均值大于等于阈值的子数组数目
 *
 * @Date 2024/11/29 16:14
 * @Author 无忧
 */
public class NumOfSubarrays {

    public static void main(String[] args) {
        int[][] arr = {{2, 2, 2, 2, 5, 5, 5, 8}, {11,13,17,23,29,31,7,5,2,3}, {1,1,1,1,1}};
        int[] k = {3, 3, 1};
        int[] threshold = {4, 5, 0};
        for(int i=2;i<arr.length;i++) {
            System.out.println(numOfSubarrays1(arr[i], k[i], threshold[i]));
        }
    }


    public static int numOfSubarrays1(int[] arr, int k, int threshold) {
        int res = 0, l = 0, r = 0, sum = 0;
        while(r < k){
            sum+=arr[r++];
        }
        int flag = threshold * k;
        res += sum >= flag ? 1 : 0;
        while(r < arr.length){
            sum = sum - arr[l++] + arr[r++];
            if(sum>=flag){
                res++;
            }
        }
        return res;
    }
    public static int numOfSubarrays(int[] arr, int k, int threshold) {
        int res = 0, l = 0, r = 0, sum = 0;
        while(r < k - 1){
            sum+=arr[r++];
        }
        int flag = threshold * k;
        while(r < arr.length){
            sum+=arr[r++];
            if(sum>=flag){
                res++;
            }
            sum = sum - arr[l++];
        }
        return res;
    }
}
